package com.zoweunion.mechanic.controller;

import com.github.pagehelper.PageInfo;
import com.zoweunion.mechanic.entity.DeptEntity;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.service.DeptService;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.Orgin;
import com.zoweunion.mechanic.util.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 部门表(新)
 *
 * @author xp
 * @email 17688825585@163.com
 * @date 2020-09-24 14:57:06
 */
@RestController
@RequestMapping("/dept")
public class DeptController extends BaseController {
    @Autowired
    private DeptService deptService;

    /**
     * 列表
     */
    @GetMapping("/deptList")
    public ResponseBean deptList(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        params.put("s_id", getCurrentUser(request).getS_id());
        params.put("pid", 0);
        List<DeptEntity> info = deptService.queryPage(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }

    /**
     * 新增或更新
     */
    @PostMapping("/deptInsertUpdate")
    public ResponseBean deptInsertUpdate(@RequestBody DeptEntity params,
                                         HttpServletRequest request) throws MyException {
        params.setSId(getCurrentUser(request).getS_id());
        if (params.getId() != null) {
            deptService.updateById(params);
        } else {
            params.setCreateTime(new Date());
            deptService.save(params);
        }
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }

    /**
     * 级联删除
     */
    @PostMapping("/deleteDept")
    public ResponseBean deleteDept(@RequestBody Map<String, Object> params,
                                         HttpServletRequest request) throws MyException {
        params.put("sid",getCurrentUser(request).getS_id());
        long id = Orgin.getLongValue(params, "id");
        params.put("pid", id);
        deptService.deleteParent(id);   // 删除当前节点(逻辑删除)
        deptService.deleteDept(params); // 删除当前节点的子节点(逻辑删除)
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }

    /**
     * 查询该账号下服务商所在区域
     */
    @GetMapping("/serverList")
    public ResponseBean serverList(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        params.put("s_id", getCurrentUser(request).getS_id());
        List<Map<String,Object>> info = deptService.serverList(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }

    /**
     * 人员新增
     */
    @PostMapping("/insertUser")
    public ResponseBean insertUser(@RequestBody DeptEntity params,
                                   HttpServletRequest request) throws MyException {
        params.setSId(getCurrentUser(request).getS_id());
        deptService.insertUser(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }

    /**
     * 人员编辑
     */
    @PostMapping("/updateUser")
    public ResponseBean updateUser(@RequestBody DeptEntity params,
                                   HttpServletRequest request) throws MyException {
        params.setSId(getCurrentUser(request).getS_id());
        deptService.updateUser(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }


    /**
     * 删除人员
     */
    @PostMapping("/deleteUser")
    public ResponseBean deleteUser(@RequestBody DeptEntity params,
                                   HttpServletRequest request) throws MyException {
        params.setSId(getCurrentUser(request).getS_id());
        deptService.deleteUser(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }

    /**
     * 人员列表
     */
    @GetMapping("/userList")
    public ResponseBean userList(@RequestParam Map<String, Object>  params,
                                   HttpServletRequest request) throws MyException {
        params.put("sid",getCurrentUser(request).getS_id());
        PageInfo<Map<String, Object>> info= deptService.userList(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }

    /**
     * 维护人员与管辖区的关系(新增前先删除)
     */
    @PostMapping("/insertUpdateServer")
    public ResponseBean insertUpdateServer(@RequestBody DeptEntity params) {
        deptService.insertUpdateServer(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }

    /**
     * 查询管辖区(根据userId获取已设置的服务商列表)
     */
    @GetMapping("/managedServerList")
    public ResponseBean getManagedServerList(@RequestParam Map<String, Object>  params){
        List<Map<String,Object>> info =  deptService.getManagedServerList(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }

    /**
     * 查询管辖区
     */
    @GetMapping("/selectServer")
    public ResponseBean selectServer(@RequestParam Map<String, Object>  params){
        List<Map<String,Object>> info =  deptService.selectServer(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }
}
